<?php

declare (strict_types=1);
namespace App\Model;

use App\Traits\ModelLabelTrait;
use Hyperf\Database\Model\SoftDeletes;
use Hyperf\DbConnection\Model\Model;
/**
 * @property int $id 
 * @property string $username 
 * @property string $password 
 * @property string $avatar 
 * @property string $email 
 * @property string $mobile 
 * @property int $status 
 * @property string $reg_time 
 * @property string $reg_ip 
 * @property string $current_login_time 
 * @property string $current_login_ip 
 * @property string $last_login_time 
 * @property string $last_login_ip 
 * @property \Carbon\Carbon $created_at 
 * @property \Carbon\Carbon $updated_at 
 * @property string $deleted_at 
 */
class User extends Model
{
    use ModelLabelTrait,SoftDeletes;
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'users';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'username',
        'password',
        'avatar',
        'email',
        'mobile',
        'status',
        'reg_time',
        'reg_ip',
        'current_login_time',
        'current_login_ip',
        'last_login_time',
        'last_login_ip',
        'user_from',
        'is_virtual',
        'union_id'
    ];

    protected $hidden=[
        'password'
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'int', 'status' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime'];

    const STATUS_OFF = 0;
    const STATUS_ON = 1;
    const STATUS_LABELS=[
      self::STATUS_OFF => '冻结',
      self::STATUS_ON => '正常'
    ];

    const USER_FROM_WECHAT = 'wechat';
    const USER_FROM_APP = 'APP';

    public static function generate_username( $length = 6,$prefix='psu_' ) {
        $prefix = config('common.user_prefix');
        // 密码字符集，可任意添加你需要的字符
        $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        do {
            $password='';
            for ( $i = 0; $i < $length; $i++ )
            {
                // 这里提供两种字符获取方式
                // 第一种是使用substr 截取$chars中的任意一位字符；
                // 第二种是取字符数组$chars 的任意元素
                // $password .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
                $password .= $chars[ mt_rand(0, strlen($chars) - 1) ];
            }
            $username = $prefix.$password;
            $record = self::where(['username'=>$username])->first();
        } while ($record);
        return $username;
    }

    public function getAvatarAttribute($field){
        return $this->attributes[$field] = $field?config('common.image_prefix').$field:config('common.image_prefix').'/images/avatar.png';
    }

    public function account(){
        return $this->hasOne(UserAccount::class,'user_id','id');
    }

    public function wechat(){
        return $this->hasOne(UserWechat::class,'user_id','id');
    }

    public function info(){
        return $this->hasOne(UserInfo::class,'user_id','id');
    }

    public function company(){
        return $this->hasOne(CompanyInfo::class,'user_id','id');
    }

    public function edu(){
        return $this->hasMany(UserEduExp::class,'user_id','id');
    }

    public function complaints(){
        return $this->morphMany(Complaint::class,'dataable');
    }
}